package com.dzt.androidnewui.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.dzt.androidnewui.fragment.Fragment1;
import com.dzt.androidnewui.fragment.Fragment2;
import com.dzt.androidnewui.fragment.Fragment3;
import com.dzt.androidnewui.fragment.FragmentAdapter;
import com.dzt.androidnewui.R;
import com.dzt.androidnewui.databinding.ActivityMainBinding;

public class MainActivity extends SuperActivity implements TabLayout.OnTabSelectedListener {

	private static final String TAG = "MainActivity";
	private ActivityMainBinding mainBinding;
	private DrawerLayout dlContent;
	private NavigationView navigationView;
	private TabLayout tabLayout;
	private Toolbar toolbar;
	private ViewPager viewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

		initResId();

		tabLayout.addOnTabSelectedListener(this);
		toolbar.setTitle(R.string.app_name);
		//toolbar.setLogo(R.mipmap.ic_launcher);
		setSupportActionBar(toolbar);
		toolbar.setNavigationIcon(R.drawable.selector_menu);
		toolbar.setNavigationOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				drawerLayoutSwitch(true);
			}
		});
		toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				switch (item.getItemId()) {
					case R.id.action_more:
						Snackbar.make(toolbar, "Click More", Snackbar.LENGTH_SHORT).show();
						break;
					case R.id.action_share:
						Snackbar.make(toolbar, "Click Share", Snackbar.LENGTH_SHORT).show();
						break;
				}
				return true;
			}
		});

		FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager());
		adapter.addFragment(new Fragment1(), "tag1");
		adapter.addFragment(new Fragment2(), "tag2");
		adapter.addFragment(new Fragment3(), "tag3");
		viewPager.setAdapter(adapter);
		tabLayout.setupWithViewPager(viewPager);

		navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
			@Override
			public boolean onNavigationItemSelected(MenuItem item) {
				Log.i(TAG, "onNavigationItemSelected = " + item.getTitle());
				switch (item.getItemId()){
					case R.id.item_green:
						break;
					case R.id.item_about:
						startActivity(AboutActivity.class, null);
						break;
					case R.id.item_exit:
						finish();
						break;
				}
				return false;
			}
		});
	}

	@Override
	protected void initParams(Bundle savedInstanceState) {

	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	private void initResId(){
		tabLayout = mainBinding.t2;
		dlContent = mainBinding.dlContent;
		toolbar = mainBinding.tb;
		viewPager = mainBinding.viewpager;
		navigationView = mainBinding.navigationView;
	}

	/**
	 * 该方法是用来加载菜单布局的
	 *
	 * @param menu
	 * @return
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//加载菜单文件
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		tabLayout.removeOnTabSelectedListener(this);
	}

	@Override
	public void onBackPressed() {
		if (dlContent.isDrawerOpen(Gravity.START)) {
			drawerLayoutSwitch(false);
		} else {
			super.onBackPressed();
		}
	}

	private void drawerLayoutSwitch(boolean isOpen) {
		if (isOpen) {
			dlContent.openDrawer(Gravity.START);
		} else {
			dlContent.closeDrawer(Gravity.START);
		}
	}

	@Override
	public void onTabSelected(TabLayout.Tab tab) {
		//Toast.makeText(MainActivity.this, tab.getText(), Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onTabUnselected(TabLayout.Tab tab) {

	}

	@Override
	public void onTabReselected(TabLayout.Tab tab) {

	}
}
